package Bean;

import java.io.*;

@SuppressWarnings("unused")
public class UserBean {
    static  String userPath="Resource/user.txt";
    static  String txPath="Resource/tx.txt";
    private String xing;
    private String ming;
    private String username;
    private String password;
    private int lineNo=-1;

    public String getTX(){
        String tx="tx.jpg";
        try {
            BufferedReader in = new BufferedReader(new FileReader(txPath));
            String str;
            String [] strs=null;
            while((str=in.readLine())!=null){
                strs=str.split("#");
                if(username.equals(strs[0])){
                    tx=strs[1];
                    in.close();
                    return tx;
                }
            }
            in.close();
        }
        catch (Exception e){

        }
        return tx;
    }
    public void setTX(String tx){
        String temp;
        try {
            File file = new File(txPath);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();

            for (int j = 0; (temp = br.readLine()) != null; j++) {
                if(j==lineNo){
                    buf = buf.append(username+"#"+tx);
                }else{
                    buf = buf.append(temp);
                }
                buf = buf.append(System.getProperty("line.separator"));
            }

            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isValid(){
        try {
            BufferedReader in = new BufferedReader(new FileReader(userPath));
            String str;
            String [] strs=null;
            while((str=in.readLine())!=null){
                strs=str.split("#");
                if(username.equals(strs[0])){
                    in.close();
                    return true;
                }
            }
            in.close();
        }
        catch (Exception e){

        }
        return false;
    }

    public boolean check(){
        try {
            BufferedReader in = new BufferedReader(new FileReader(userPath));
            String str;
            String [] strs=null;
            while((str=in.readLine())!=null){
                strs=str.split("#");
                if(username.equals(strs[0])&&password.equals(strs[1])){
                    in.close();
                    return true;
                }
            }
            in.close();
        }
        catch (Exception e){

        }
        return false;
    }

    public void save(){
        String temp;
        try {
            File file = new File(userPath);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();

            for (int j = 0; (temp = br.readLine()) != null; j++) {
                if(j==lineNo){
                    buf = buf.append(username+"#"+password+"#"+xing+"#"+ming);
                }else{
                    buf = buf.append(temp);
                }
                buf = buf.append(System.getProperty("line.separator"));
            }
            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public boolean signup(){
        try {
            FileWriter userFlie = new FileWriter(userPath,true);
            userFlie.write(username+"#"+password+"#"+xing+"#"+ming+"\n");
            userFlie.close();
            FileWriter txFile = new FileWriter(txPath,true);
            txFile.write(username+"#"+"\n");
            txFile.close();
        }
        catch (Exception e){

        }
        return true;
    }

    public boolean login(){
        try {
            BufferedReader in = new BufferedReader(new FileReader(userPath));
            String str;
            String [] strs=null;
            for(int j=0;(str=in.readLine())!=null;j++){
                strs=str.split("#");
                if(username.equals(strs[0])&&password.equals(strs[1])){
                    lineNo=j;
                    xing=strs[2];
                    ming=strs[3];
                    in.close();
                    return true;
                }
            }
            in.close();
        }
        catch (Exception e){

        }
        return false;
    }

    public UserBean(String xing, String ming, String username, String password) {
        this.xing = xing;
        this.ming = ming;
        this.username = username;
        this.password = password;
    }

    public UserBean() {
    }

    public String getXing() {
        return xing;
    }

    public void setXing(String xing) {
        this.xing = xing;
    }

    public String getMing() {
        return ming;
    }

    public void setMing(String ming) {
        this.ming = ming;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getLineNo() { return lineNo; }

    public void setLineNo(int lineNo) { this.lineNo = lineNo; }
}
